package com.hunliji.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hunliji.common.response.PageData;
import com.hunliji.monitor.dao.mapper.mt.BusinessTableRecordMapper;
import com.hunliji.monitor.dao.model.dataobject.BusinessTableRecordDO;
import com.hunliji.monitor.service.api.BusinessTableRecordService;
import com.hunliji.monitor.service.api.model.bo.BusinessTableRecordBO;
import com.hunliji.monitor.service.api.model.param.QueryBusinessTableRecordParam;
import com.hunliji.monitor.share.component.ComponentLocator;
import com.hunliji.monitor.share.utils.PageDataUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 业务表记录
 * @author wuchen
 */
@Service
public class BusinessTableRecordServiceImpl implements BusinessTableRecordService {
    @Resource
    private BusinessTableRecordMapper businessTableRecordMapper;
    @Override
    public BusinessTableRecordBO add(BusinessTableRecordBO businessTableRecordBO) {
        BusinessTableRecordDO businessTableRecordDO = ComponentLocator.getConverter().convert(businessTableRecordBO,BusinessTableRecordDO.class);
        businessTableRecordMapper.insert(businessTableRecordDO);
        businessTableRecordBO.setId(businessTableRecordDO.getId());
        return businessTableRecordBO;
    }

    @Override
    public void update(BusinessTableRecordBO businessTableRecordBO) {
        BusinessTableRecordDO businessTableRecordDO = ComponentLocator.getConverter().convert(businessTableRecordBO,BusinessTableRecordDO.class);
        businessTableRecordMapper.updateById(businessTableRecordDO);
    }

    @Override
    public PageData<BusinessTableRecordBO> queryPage(QueryBusinessTableRecordParam queryBusinessTableRecordParam) {
        LambdaQueryWrapper<BusinessTableRecordDO> queryWrapper = new LambdaQueryWrapper<BusinessTableRecordDO>();
        if(StringUtils.isNotBlank(queryBusinessTableRecordParam.getMatchTableName())){
            queryWrapper.like(BusinessTableRecordDO::getTableName,queryBusinessTableRecordParam.getMatchTableName());
        }
        if(StringUtils.isNotBlank(queryBusinessTableRecordParam.getDbName())){
            queryWrapper.like(BusinessTableRecordDO::getDatabaseName,queryBusinessTableRecordParam.getDbName());
        }
        if(!Objects.isNull(queryBusinessTableRecordParam.getId())){
            queryWrapper.eq( BusinessTableRecordDO::getId,queryBusinessTableRecordParam.getId());
        }
        if(StringUtils.isNotBlank(queryBusinessTableRecordParam.getRemark())){
            queryWrapper.like(BusinessTableRecordDO::getBusinessRemarks,queryBusinessTableRecordParam.getRemark());
        }
        queryWrapper.eq(BusinessTableRecordDO::getDeleted,Boolean.FALSE);
        queryWrapper.orderByDesc(BusinessTableRecordDO::getUpdatedAt);
        PageHelper.startPage(queryBusinessTableRecordParam.getPageNum(),queryBusinessTableRecordParam.getPageSize());
        List<BusinessTableRecordDO> businessTableRecordDOList = businessTableRecordMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(businessTableRecordDOList)){
            PageInfo<BusinessTableRecordDO> pageInfo = new PageInfo<>(businessTableRecordDOList);
            List<BusinessTableRecordBO> businessTableRecordBOList = ComponentLocator.getConverter().convertList(businessTableRecordDOList,BusinessTableRecordBO.class);
            return PageDataUtil.getPageData((int)pageInfo.getTotal(),businessTableRecordBOList);
        }
        return PageDataUtil.getEmptyData();
    }

    @Override
    public List<BusinessTableRecordBO> query(QueryBusinessTableRecordParam queryBusinessTableRecordParam) {
        LambdaQueryWrapper<BusinessTableRecordDO> queryWrapper = new LambdaQueryWrapper<BusinessTableRecordDO>();
        if(StringUtils.isNotBlank(queryBusinessTableRecordParam.getMatchTableName())){
            queryWrapper.like(BusinessTableRecordDO::getTableName,queryBusinessTableRecordParam.getMatchTableName());
        }
        if(Objects.nonNull(queryBusinessTableRecordParam.getId())){
            queryWrapper.eq( BusinessTableRecordDO::getId,queryBusinessTableRecordParam.getId());
        }
        if(!CollectionUtils.isEmpty(queryBusinessTableRecordParam.getIds())){
            queryWrapper.in(BusinessTableRecordDO::getId,queryBusinessTableRecordParam.getIds());
        }
        if(StringUtils.isNotBlank(queryBusinessTableRecordParam.getTableName())){
            queryWrapper.eq(BusinessTableRecordDO::getTableName,queryBusinessTableRecordParam.getTableName());
        }
        if(StringUtils.isNotBlank(queryBusinessTableRecordParam.getDbName())){
            queryWrapper.eq(BusinessTableRecordDO::getDatabaseName,queryBusinessTableRecordParam.getDbName());
        }
        if(StringUtils.isNotBlank(queryBusinessTableRecordParam.getRemark())){
            queryWrapper.eq(BusinessTableRecordDO::getBusinessRemarks,queryBusinessTableRecordParam.getRemark());
        }
        queryWrapper.eq(BusinessTableRecordDO::getDeleted,Boolean.FALSE);
        queryWrapper.orderByDesc(BusinessTableRecordDO::getUpdatedAt);
        List<BusinessTableRecordDO> businessTableRecordDOList = businessTableRecordMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(businessTableRecordDOList)){
            return  ComponentLocator.getConverter().convertList(businessTableRecordDOList,BusinessTableRecordBO.class);
        }
        return new ArrayList<>();
    }

    @Override
    public BusinessTableRecordBO queryById(Long id) {
        BusinessTableRecordDO businessTableRecordDO = businessTableRecordMapper.selectById(id);
        if(Objects.isNull(businessTableRecordDO)){
            return null;
        }
        return ComponentLocator.getConverter().convert(businessTableRecordDO,BusinessTableRecordBO.class);
    }
}
